﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Lifetime;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RemotingLibs.QuoteClass;

namespace RemotingLibs
{
    public class RemotingBase : MarshalByRefObject
    {
        public override object InitializeLifetimeService()
        {
            var aLease = (ILease)base.InitializeLifetimeService();

            if (aLease != null && aLease.CurrentState == LeaseState.Initial)
            {
                // 不过期
                aLease.InitialLeaseTime = TimeSpan.Zero;
            }
            return aLease;
        }

        #region 对象JSON化

        /// <summary>
        /// 把object对象JSON化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string ObjectToJson<T>(T obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        #endregion

        #region 还原Redis中的JSON化对象

        /// <summary>
        /// 还原Redis中的JSON化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public T JsonToObject<T>(string json)
        {
            if (json == null)
                json = ObjectToJson(default(T));
            return JsonConvert.DeserializeObject<T>(json);
        }

        #endregion

        #region

        public class JsonValueEventArg : EventArgs
        {
            /// <summary>
            /// JsonValue原始类型Id，需要服务器端和客户端两端协商确定
            /// 0——一般信息通知
            /// 10001——通知客户Login
            /// 10002——通知客户Logout
            /// 10003——通知保存收盘价
            /// 10004——通知转换今仓为昨仓
            /// </summary>
            public long TypeId { get; set; }

            /// <summary>
            /// 要传输的数据的JSON格式
            /// </summary>
            public string JsonValue { get; set; }
        }

        /// <summary>
        /// 服务器接收数据事件代理
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public delegate string JsonValueEventHandler(JsonValueEventArg e);

        /// <summary>
        /// 服务器接收数据事件
        /// </summary>
        public event JsonValueEventHandler OnJsonValueEvent;

        /// <summary>
        /// 客户端触发传输数据
        /// </summary>
        /// <param name="jsonValue"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public string SendJson(string jsonValue, long typeId)
        {
            if (OnJsonValueEvent != null)
            {
                return OnJsonValueEvent(new JsonValueEventArg { JsonValue = jsonValue, TypeId = typeId });
            }

            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
        #endregion

        #region 通达信返回

        public class RsqTdxEventArg : EventArgs
        {
            public TdxFun TypeId { get; set; }
            public string ReqTdx { get; set; }
        }

        /// <summary>
        /// 服务器接收数据事件代理
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public delegate string RspTdxEventArgEventHandler(RsqTdxEventArg e);

        /// <summary>
        /// 服务器接收数据事件
        /// </summary>
        public event RspTdxEventArgEventHandler OnRspTdxEvent;

        public string ReqTdx(ReqTdx reqobj, TdxFun typeId)
        {
            if (OnRspTdxEvent != null)
            {
                return OnRspTdxEvent(new RsqTdxEventArg { ReqTdx = ObjectToJson(reqobj), TypeId = typeId });
            }
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
        #endregion


        #region 通达信交易返回

        public class RsqTdxTradeEventArg : EventArgs
        {
            public TdxFun TypeId { get; set; }
            public string ReqTdx { get; set; }
        }

        /// <summary>
        /// 服务器接收数据事件代理
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public delegate string RspTdxTradeEventArgEventHandler(RsqTdxTradeEventArg e);

        /// <summary>
        /// 服务器接收数据事件
        /// </summary>
        public event RspTdxTradeEventArgEventHandler OnRspTdxTradeEvent;

        public string ReqTdxTrade(ReqTdxTrade reqobj, TdxFun typeId)
        {
            if (OnRspTdxTradeEvent != null)
            {
                return OnRspTdxTradeEvent(new RsqTdxTradeEventArg { ReqTdx = ObjectToJson(reqobj), TypeId = typeId });
            }

            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
        #endregion


        #region 同花顺返回
        public class RsqThsEventArg : EventArgs
        {
 
            public long TypeId { get; set; }
            public string ReqThs { get; set; }
        }

        /// <summary>
        /// 服务器接收数据事件代理
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public delegate string RspThsEventHandler(RsqThsEventArg e);

        /// <summary>
        /// 服务器接收数据事件
        /// </summary>
        public event RspThsEventHandler OnRspThsEvent;

        /// <summary>
        /// 客户端触发传输数据
        /// </summary>
        /// <param name="jsonValue"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public string ReqThs(ReqThs reqobj, long typeId)
        {
            if (OnRspThsEvent != null)
            {
                return OnRspThsEvent(new RsqThsEventArg { ReqThs = ObjectToJson(reqobj), TypeId = typeId });
            }

            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
        #endregion


    }

}

